package com.mj.haier.rrskx.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mj.haier.rrskx.domain.CarsInfo;
import com.mj.haier.rrskx.repository.CarsInfoRepository;
import com.mj.haier.rrskx.repository.search.CarsInfoSearchRepository;
import com.mj.haier.rrskx.web.rest.errors.BadRequestAlertException;
import com.mj.haier.rrskx.web.rest.util.HeaderUtil;
import com.mj.haier.rrskx.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing CarsInfo.
 */
@RestController
@RequestMapping("/api")
public class CarsInfoResource {

    private final Logger log = LoggerFactory.getLogger(CarsInfoResource.class);

    private static final String ENTITY_NAME = "microRrskxCarsInfo";

    private final CarsInfoRepository carsInfoRepository;

    private final CarsInfoSearchRepository carsInfoSearchRepository;

    public CarsInfoResource(CarsInfoRepository carsInfoRepository, CarsInfoSearchRepository carsInfoSearchRepository) {
        this.carsInfoRepository = carsInfoRepository;
        this.carsInfoSearchRepository = carsInfoSearchRepository;
    }

    /**
     * POST  /cars-infos : Create a new carsInfo.
     *
     * @param carsInfo the carsInfo to create
     * @return the ResponseEntity with status 201 (Created) and with body the new carsInfo, or with status 400 (Bad Request) if the carsInfo has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    // @PostMapping("/cars-infos")
    // @Timed
    // public ResponseEntity<CarsInfo> createCarsInfo(@RequestBody CarsInfo carsInfo) throws URISyntaxException {
    //     log.debug("REST request to save CarsInfo : {}", carsInfo);
    //     if (carsInfo.getCarsId() != null) {
    //         throw new BadRequestAlertException("A new carsInfo cannot already have an CarsId", ENTITY_NAME, "carsidexists");
    //     }
    //     CarsInfo result = carsInfoRepository.save(carsInfo);
    //     carsInfoSearchRepository.save(result);
    //     return ResponseEntity.created(new URI("/api/cars-infos/" + result.getCarsId()))
    //         .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getCarsId().toString()))
    //         .body(result);
    // }

    /**
     * PUT  /cars-infos : Updates an existing carsInfo.
     *
     * @param carsInfo the carsInfo to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated carsInfo,
     * or with status 400 (Bad Request) if the carsInfo is not valid,
     * or with status 500 (Internal Server Error) if the carsInfo couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    // @PutMapping("/cars-infos")
    // @Timed
    // public ResponseEntity<CarsInfo> updateCarsInfo(@RequestBody CarsInfo carsInfo) throws URISyntaxException {
    //     log.debug("REST request to update CarsInfo : {}", carsInfo);
    //     if (carsInfo.getCarsId() == null) {
    //         throw new BadRequestAlertException("Invalid carsid", ENTITY_NAME, "carsidnull");
    //     }
    //     CarsInfo result = carsInfoRepository.save(carsInfo);
    //     carsInfoSearchRepository.save(result);
    //     return ResponseEntity.ok()
    //         .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, carsInfo.getCarsId().toString()))
    //         .body(result);
    // }

    /**
     * GET  /cars-infos : get all the carsInfos.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of carsInfos in body
     */
    @GetMapping("/cars-infos")
    @Timed
    public ResponseEntity<List<CarsInfo>> getAllCarsInfos(Pageable pageable) {
        log.debug("REST request to get a page of CarsInfos");
        Page<CarsInfo> page = carsInfoRepository.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/cars-infos");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
     * GET  /cars-infos/:carsId : get the "carsId" carsInfo.
     *
     * @param carsId the carsId of the carsInfo to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the carsInfo, or with status 404 (Not Found)
     */
    @GetMapping("/cars-infos/{carsId}")
    @Timed
    public ResponseEntity<CarsInfo> getCarsInfo(@PathVariable String carsId) {
        log.debug("REST request to get CarsInfo : {}", carsId);
        Optional<CarsInfo> carsInfo = carsInfoRepository.findByCarsId(carsId);
        return ResponseUtil.wrapOrNotFound(carsInfo);
    }

    /**
     * DELETE  /cars-infos/:carsId : delete the "carsId" carsInfo.
     *
     * @param carsId the carsId of the carsInfo to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    // @DeleteMapping("/cars-infos/{carsId}")
    // @Timed
    // public ResponseEntity<Void> deleteCarsInfo(@PathVariable String carsId) {
    //     log.debug("REST request to delete CarsInfo : {}", carsId);

    //     carsInfoRepository.deleteByCarsId(carsId);
    //     carsInfoSearchRepository.deleteByCarsId(carsId);
    //     return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, carsId.toString())).build();
    // }

    /**
     * SEARCH  /_search/cars-infos?query=:query : search for the carsInfo corresponding
     * to the query.
     *
     * @param query the query of the carsInfo search
     * @param pageable the pagination information
     * @return the result of the search
     */
    // @GetMapping("/_search/cars-infos")
    // @Timed
    // public ResponseEntity<List<CarsInfo>> searchCarsInfos(@RequestParam String query, Pageable pageable) {
    //     log.debug("REST request to search for a page of CarsInfos for query {}", query);
    //     Page<CarsInfo> page = carsInfoSearchRepository.search(queryStringQuery(query), pageable);
    //     HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/cars-infos");
    //     return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    // }

}
